package grafica;

import hotel.Autenticazione;
import hotel.Database;

import java.awt.EventQueue;

import javax.swing.*;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import javax.swing.border.BevelBorder;

import server.AutenticaRegistrazione;

import java.awt.Point;
import java.awt.event.*;
import java.io.IOException;
import java.sql.SQLException;

public class RecuperoUsernameGrafica {

	private JFrame frmRecuperoUsername;
	private JPasswordField passwordT;
	private JTextField emailT;
	private Database d;
	private JLabel password;
	private JButton recUser;
	private SpringLayout springLayout;
	private JLabel caricamento;

	public RecuperoUsernameGrafica(Database d) {
		this.d = d;
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frmRecuperoUsername = new JFrame();
		frmRecuperoUsername.setLocation(new Point(450, 100));
		String look = UIManager.getSystemLookAndFeelClassName();
		try {
			UIManager.setLookAndFeel(look);
		} catch (ClassNotFoundException | InstantiationException
				| IllegalAccessException | UnsupportedLookAndFeelException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		frmRecuperoUsername.getContentPane().setBackground(Color.LIGHT_GRAY);
		frmRecuperoUsername.setBackground(Color.LIGHT_GRAY);
		frmRecuperoUsername.setVisible(true);
		frmRecuperoUsername.setTitle("Recupero Username");
		frmRecuperoUsername.setSize(new Dimension(350, 250));
		frmRecuperoUsername.setResizable(false);
		frmRecuperoUsername
				.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		frmRecuperoUsername.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				new AccessoGrafica(d);
				frmRecuperoUsername.dispose();

			}
		});
		springLayout = new SpringLayout();
		frmRecuperoUsername.getContentPane().setLayout(springLayout);

		password = new JLabel("Password");
		springLayout.putConstraint(SpringLayout.WEST, password, 142, SpringLayout.WEST, frmRecuperoUsername.getContentPane());
		password.setHorizontalAlignment(SwingConstants.CENTER);
		password.setAlignmentX(Component.CENTER_ALIGNMENT);
		password.setFont(new Font("Times New Roman", Font.PLAIN, 16));
		frmRecuperoUsername.getContentPane().add(password);

		JLabel email = new JLabel("Email");
		springLayout.putConstraint(SpringLayout.WEST, email, 154, SpringLayout.WEST, frmRecuperoUsername.getContentPane());
		email.setFont(new Font("Times New Roman", Font.PLAIN, 16));
		frmRecuperoUsername.getContentPane().add(email);

		passwordT = new JPasswordField();
		springLayout.putConstraint(SpringLayout.SOUTH, password, -6, SpringLayout.NORTH, passwordT);
		springLayout.putConstraint(SpringLayout.NORTH, passwordT, 35,
				SpringLayout.NORTH, frmRecuperoUsername.getContentPane());
		springLayout.putConstraint(SpringLayout.WEST, passwordT, 104,
				SpringLayout.WEST, frmRecuperoUsername.getContentPane());
		springLayout.putConstraint(SpringLayout.EAST, passwordT, -105,
				SpringLayout.EAST, frmRecuperoUsername.getContentPane());
		passwordT.setFont(new Font("Times New Roman", Font.PLAIN, 14));
		passwordT.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null,
				null, null));
		frmRecuperoUsername.getContentPane().add(passwordT);
		passwordT.setColumns(10);

		emailT = new JTextField();
		springLayout.putConstraint(SpringLayout.NORTH, emailT, 84, SpringLayout.NORTH, frmRecuperoUsername.getContentPane());
		springLayout.putConstraint(SpringLayout.SOUTH, email, -6, SpringLayout.NORTH, emailT);
		springLayout.putConstraint(SpringLayout.WEST, emailT, 104, SpringLayout.WEST, frmRecuperoUsername.getContentPane());
		springLayout.putConstraint(SpringLayout.EAST, emailT, -105, SpringLayout.EAST, frmRecuperoUsername.getContentPane());
		emailT.setFont(new Font("Times New Roman", Font.PLAIN, 14));
		emailT.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null,
				null));
		frmRecuperoUsername.getContentPane().add(emailT);
		emailT.setColumns(10);

		recUser = new JButton("Recupera Username");
		springLayout.putConstraint(SpringLayout.WEST, recUser, 0,
				SpringLayout.WEST, passwordT);
		springLayout.putConstraint(SpringLayout.SOUTH, recUser, -28,
				SpringLayout.SOUTH, frmRecuperoUsername.getContentPane());
		recUser.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				String pass = String.valueOf(passwordT.getPassword());
				if (pass.equals("")) {
					JOptionPane.showMessageDialog(null,
							"Inserisci la Password!");
				} else if (emailT.getText().equals("")) {
					JOptionPane.showMessageDialog(null, "Inserisci l'Email!");
				} else {
					attivaCaricamento();
					new Load(pass, emailT.getText(), frmRecuperoUsername, d).start();
				}
			}

		});
		recUser.setFont(new Font("Times New Roman", Font.BOLD, 12));
		recUser.setBackground(Color.LIGHT_GRAY);
		frmRecuperoUsername.getContentPane().add(recUser);

		ImageIcon imageIcon = new ImageIcon("Risorse/indicator.gif");
		caricamento = new JLabel("Connessione al server...", imageIcon,
				JLabel.CENTER);
		springLayout.putConstraint(SpringLayout.WEST, caricamento, 0,
				SpringLayout.WEST, passwordT);
		springLayout.putConstraint(SpringLayout.SOUTH, caricamento, -25,
				SpringLayout.NORTH, recUser);
		frmRecuperoUsername.getContentPane().add(caricamento);
		caricamento.setVisible(false);

	}



	public void attivaCaricamento() {
		caricamento.setVisible(true);
		recUser.setVisible(false);
	}

	public void disattivaCaricamento() {
		caricamento.setVisible(false);
		recUser.setVisible(true);
	}

	class Load extends Thread {
		private String pass, email;
		private JFrame r;
		private Database d;

		public Load(String pass, String email, JFrame r, Database d) {
			this.pass = pass;
			this.email = email;
			this.r = r;
			this.d = d;
		}

		public void run() {
			try {
				if (d.recuperaUsername(pass, email)) {
					new AccessoGrafica(d);
					r.dispose();
				}
				disattivaCaricamento();
			} catch (Exception e) {
				disattivaCaricamento();
			}
		}
	}
}
